/**
 *
 */
package com.hitangjun.utils;

/**
 * @author Administrator
 */
public class RxConvertHelper {
    /**
     * @author Administrator
     */
    public static int byteArrayToInt(byte[] a) {
        int mask = 0xFF;
        int result = 0;
        result = a[0] & mask;
        result = result + ((a[1] & mask) << 8);
        result = result + ((a[2] & mask) << 16);
        result = result + ((a[3] & mask) << 24);
        return result;
    }

    public static int byteArrayToInt3(byte[] a) {
        int mask = 0xFF;
        int result = 0;
        result = a[0] & mask;
        result = result + ((a[1] & mask) << 8);
        result = result + ((a[2] & mask) << 16);
        return result;
    }

    public static int byteArrayToInt2(byte[] a) {
        int mask = 0xFF;
        int result = 0;
        result = a[0] & mask;
        result = result + ((a[1] & mask) << 8);
        return result;
    }

    public static int bigByteArrayToInt2(byte[] a) {
        int mask = 0xFF;
        int result = 0;
        result = a[1] & mask;
        result = result + ((a[0] & mask) << 8);
        return result;
    }

    public static short byteArrayToShort(byte[] a) {
        int mask = 0xff;
        int result = 0;
        result = (a[0] & mask);
        result = (result + ((a[1] & mask) << 8));
        return (short) result;
    }

    public static short byteArrayToSign(byte a) {
        int mask = 0xff;
        int result = 0;
        result = (a & mask);
        return (short) result;
    }

    public static byte[] intToByteArray(int a) {
        byte[] ret = new byte[4];
        ret[0] = (byte) (a & 0xFF);
        ret[1] = (byte) ((a >> 8) & 0xFF);
        ret[2] = (byte) ((a >> 16) & 0xFF);
        ret[3] = (byte) ((a >> 24) & 0xFF);
        return ret;
    }

    public static byte intToByte(int a) {
        byte[] ret = new byte[4];
        ret[0] = (byte) (a & 0xFF);
        return ret[0];
    }

    public static byte[] intToByteArray2(int a) {
        byte[] ret = new byte[2];
        ret[1] = (byte) (a & 0xFF);
        ret[0] = (byte) ((a >> 8) & 0xFF);
        return ret;
    }

    public static byte[] intToByteArray3(int a) {
        byte[] ret = new byte[3];
        ret[0] = (byte) (a & 0xFF);
        ret[1] = (byte) ((a >> 8) & 0xFF);
        ret[2] = (byte) ((a >> 16) & 0xFF);
        return ret;
    }

    public static byte[] intToSigleByteArray(int a) {
        byte[] ret = new byte[1];
        ret[0] = (byte) (a & 0xFF);
        return ret;
    }

    public static byte[] shortToByteArray(short a) {
        byte[] ret = new byte[2];
        ret[0] = (byte) (a & 0xFF);
        ret[1] = (byte) ((a >> 8) & 0xFF);
        return ret;
    }

    public static byte[] shortToByteArray2(short a) {
        byte[] ret = new byte[2];
        ret[1] = (byte) (a & 0xFF);
        ret[0] = (byte) ((a >> 8) & 0xFF);
        return ret;
    }

    public static byte shortToByte(short a) {
        byte[] ret = new byte[2];
        ret[0] = (byte) (a & 0xFF);
        ret[1] = (byte) ((a >> 8) & 0xFF);
        return ret[0];
    }

    public static byte[] convertHexString(String ss) {
        byte digest[] = new byte[ss.length() / 2];
        for (int i = 0; i < digest.length; i++) {
            String byteString = ss.substring(2 * i, 2 * i + 2);
            int byteValue = Integer.parseInt(byteString, 16);
            digest[i] = (byte) byteValue;
        }

        return digest;
    }

    /**
     * Convert hex string to byte[]
     *
     * @param hexString the hex string
     * @return byte[]
     */
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    /**
     * Convert char to byte
     *
     * @param c char
     * @return byte
     */
    private static byte charToByte(char c) {
        int index = "0123456789ABCDEF".indexOf(c);
        if (index == -1) {
            index = "0123456789abcdef".indexOf(c);
        }
        return (byte) index;
    }

    /**
     * bytes转换成十六进制字符串
     *
     * @param b byte数组
     * @return String 每个Byte值之间空格分隔
     */
    public static String byte2HexStr(byte[] b) {
        String stmp = "";
        StringBuilder sb = new StringBuilder("");
        if (b == null || b.length == 0) {
            return "";
        }
        for (int n = 0; n < b.length; n++) {
            stmp = Integer.toHexString(b[n] & 0xFF);
            sb.append((stmp.length() == 1) ? "0" + stmp : stmp);
            sb.append(" ");
        }
        return sb.toString().toUpperCase().trim();
    }

    /**
     * bytes转换成十六进制字符串
     *
     * @param b byte数组
     * @return String 每个Byte值之间空格分隔
     */
    public static String byte2HexStrNoBlank(byte[] b) {
        String stmp = "";
        StringBuilder sb = new StringBuilder("");
        if (b == null || b.length == 0) {
            return "";
        }
        for (int n = 0; n < b.length; n++) {
            stmp = Integer.toHexString(b[n] & 0xFF);
            sb.append((stmp.length() == 1) ? "0" + stmp : stmp);
        }
        return sb.toString().toUpperCase().trim();
    }

    public static String toHexString(byte b[]) {
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            String plainText = Integer.toHexString(0xff & b[i]);
            if (plainText.length() < 2)
                plainText = "0" + plainText;
            hexString.append(plainText);
        }

        return hexString.toString();
    }

    /**
     * 字符串转换成十六进制字符串
     *
     * @param str 待转换的ASCII字符串
     * @return String 每个Byte之间空格分隔，如: [61 6C 6B]
     */
    public static String str2HexStr(String str) {

        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = str.getBytes();
        int bit;

        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f;
            sb.append(chars[bit]);
            sb.append(' ');
        }
        return sb.toString().trim();
    }

    /**
     * Copy the data from @param data2 to @param data1.
     *
     * @param data1      target data
     * @param data2      source data
     * @param startIndex from which index to copy
     */
    public static synchronized byte[] CombineByteArray(byte[] data1, byte[] data2, int startIndex) {
        try {
            for (int i = 0; i < data2.length; i++) {
                data1[startIndex + i] = data2[i];
            }
            return data1;
        } catch (Exception e) {
            e.getMessage();
        }
        return null;
    }

    /**
     * TODO：截取一定长度的 byte【】
     *
     * @param data1      新数据
     * @param data2      需要被合并的数据
     * @param startIndex
     * @param length     截取数据长度
     * @author zhengjunyang
     * @version
     */
    public static synchronized void substractByteArrayWithEnd(byte[] data1, byte[] data2, int
            startIndex, int length) {
        try {
            if (data1 == null || data2 == null) {
                return;
            }
            if (length > data2.length) {
                return;
            }
            for (int i = 0; i < length; i++) {
                data2[i] = data1[startIndex + i];
            }
        } catch (Exception e) {
            e.getMessage();
        }
    }

    /**
     * <pre>
     *     desc   :long类型转成byte数组
     * </pre>
     */
    public static byte[] longToByte(long number) {
        long temp = number;
        byte[] b = new byte[2];
        for (int i = 0; i < b.length; i++) {
            b[i] = new Long(temp & 0xff).byteValue();//
            temp = temp >> 8; // 向右移8位
        }
        return b;
    }

    public static String IntToHex(int n) {
        char[] ch = new char[20];
        int nIndex = 0;
        while (true) {
            int m = n / 16;
            int k = n % 16;
            if (k == 15)
                ch[nIndex] = 'F';
            else if (k == 14)
                ch[nIndex] = 'E';
            else if (k == 13)
                ch[nIndex] = 'D';
            else if (k == 12)
                ch[nIndex] = 'C';
            else if (k == 11)
                ch[nIndex] = 'B';
            else if (k == 10)
                ch[nIndex] = 'A';
            else
                ch[nIndex] = (char) ('0' + k);
            nIndex++;
            if (m == 0)
                break;
            n = m;
        }
        StringBuffer sb = new StringBuffer();
        sb.append(ch, 0, nIndex);
        sb.reverse();
        String strHex = new String("");
        strHex += sb.toString();
        return strHex;
    }

    //16进制转10进制
    public static int HexToInt(String strHex) {
        int nResult = 0;
        if (!IsHex(strHex))
            return nResult;
        String str = strHex.toUpperCase();
        if (str.length() > 2) {
            if (str.charAt(0) == '0' && str.charAt(1) == 'X') {
                str = str.substring(2);
            }
        }
        int nLen = str.length();
        for (int i = 0; i < nLen; ++i) {
            char ch = str.charAt(nLen - i - 1);
            try {
                nResult += (GetHex(ch) * GetPower(16, i));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return nResult;
    }

    //计算16进制对应的数值
    public static int GetHex(char ch) throws Exception {
        if (ch >= '0' && ch <= '9')
            return (int) (ch - '0');
        if (ch >= 'a' && ch <= 'f')
            return (int) (ch - 'a' + 10);
        if (ch >= 'A' && ch <= 'F')
            return (int) (ch - 'A' + 10);
        throw new Exception("error param");
    }

    //计算幂
    public static int GetPower(int nValue, int nCount) throws Exception {
        if (nCount < 0)
            throw new Exception("nCount can't small than 1!");
        if (nCount == 0)
            return 1;
        int nSum = 1;
        for (int i = 0; i < nCount; ++i) {
            nSum = nSum * nValue;
        }
        return nSum;
    }

    //判断是否是16进制数
    public static boolean IsHex(String strHex) {
        int i = 0;
        if (strHex.length() > 2) {
            if (strHex.charAt(0) == '0' && (strHex.charAt(1) == 'X' || strHex.charAt(1) == 'x')) {
                i = 2;
            }
        }
        for (; i < strHex.length(); ++i) {
            char ch = strHex.charAt(i);
            if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F') || (ch >= 'a' && ch <= 'f'))
                continue;
            return false;
        }
        return true;
    }
}
